package com.ruoyi.threads;

/**
 * @Author syc
 * @Date Created in 16:36 2022/8/10
 * @Description
 */
// 模拟龟兔赛跑
public class WgTzRun implements  Runnable{
    // 1. 首先来个赛道距离,然后要离重点越来越近
    // 2. 判断比赛是否结束
    // 3. 打印出胜利者
    // 4. 龟兔赛跑开始
    // 5. 故事中是乌龟赢的,梯子需要睡觉,所以我们来模拟兔子睡觉
    // 6. 终于,乌龟赢了比赛
    private String result;

    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {
           if(Thread.currentThread().getName().equals("兔子") && i == 95) {
               try {
                   Thread.sleep(10);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
           boolean  bol =  over(i);
           if(bol) {
               break;
           }
            System.out.println(Thread.currentThread().getName() + "-->跑了"+ i +"步");

        }

    }

    public boolean over(int stops) {
       if(result != null) {
          return true;
       }
       if (stops >= 100) {
           result = Thread.currentThread().getName();
           System.out.println(Thread.currentThread().getName() +"胜利了");
           return true;
       }
       return false;
    }


    public static void main(String[] args) {
        WgTzRun wgTzRun = new WgTzRun();
        new Thread(wgTzRun, "兔子").start();
        new Thread(wgTzRun,"乌龟").start();

    }




}
